package com.atguigu.tingshu.user.startegy;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author：yyj
 * @Package：com.atguigu.tingshu.user.startegy
 * @Project：tingshu_parent240829
 * @name：ProcessVipOrderTypePayRecord
 * @Date：2025/2/13 16:32
 * @Filename：ProcessVipOrderTypePayRecord
 */
@Component("1003")
@Slf4j
public class ProcessVipOrderTypePayRecord implements ProcessDiffPayRecord{
    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void processPayRecord(UserPaidRecordVo userPaidRecordVo) {
        // user_vip_service表中插入数据
        String orderNo = userPaidRecordVo.getOrderNo();
        // 1.查询做幂等性校验
        UserVipService userVipService = userVipServiceMapper.selectOne(new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, orderNo));
        if (userVipService != null) {
            throw new GuiguException(201, "该vip类型订单支付流水已存在");
        }

        //根据用户id 查询用户信息;
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        Integer isVip = userInfo.getIsVip();
        Date vipExpireTime = userInfo.getVipExpireTime();
        //日历
        Calendar instance = Calendar.getInstance();
        if(isVip==0){
            instance.setTime(new Date());
        }else {
            if (vipExpireTime.before(new Date())){
                instance.setTime(new Date());
            }else {
                instance.setTime(vipExpireTime);
            }
        }
        userVipService = new UserVipService();
        userVipService.setOrderNo(orderNo);

        // 2.1 根据vip套餐id 查询vip套餐信息
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        //套餐的时间
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        instance.add(Calendar.MONTH,serviceMonth);
        userVipService.setStartTime(new Date());
        userVipService.setExpireTime(instance.getTime());
        int insert = userVipServiceMapper.insert(userVipService);
        log.info("保存订单：{}，记录支付流水:{}", orderNo, insert > 1 ? "成功" : "失败");
        // 4.修改用户信息（vip身份 过期时间）
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        int count = userInfoMapper.updateById(userInfo);
        log.info("修改用户信息{}", count > 1 ? "成功" : "失败");
    }
}